package com.nuedu.corejava.ch12;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class TestStudent {
	/**
	 * 保存学生信息的文件
	 */
	public static final String FILE_NAME = "c:\\student.data";

	// 编写一段代码，从键盘输入3个学生的信息，并保存到文件中
	public static void inputStundentInfo() {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入3个学生的信息");
		List<Student> list = new ArrayList<Student>();
		Student student = null;
		for (int i = 0; i < 3; i++) {
			System.out.println("请输入学号（数字）");
			String idsString = scanner.nextLine();
			int id = Integer.parseInt(idsString);
			System.out.println("请输入姓名");
			String name = scanner.nextLine();

			System.out.println("请输入年龄");
			String agesString = scanner.nextLine();
			int age = Integer.parseInt(agesString);

			System.out.println("请输入性别");
			String sex = scanner.nextLine();
			student = new Student(id, name, age, sex);
			list.add(student);
		}
		File file = new File(FILE_NAME);
		FileOutputStream fileOutputStream = null;
		ObjectOutputStream objectOutputStream = null;
		try {
			if (!file.exists()) {
				file.getParentFile().mkdirs();
				file.createNewFile();
			}
			fileOutputStream = new FileOutputStream(file);// 操作文件- 节点流
			objectOutputStream = new ObjectOutputStream(fileOutputStream);// 对流进行包装转换- 处理流
			objectOutputStream.writeObject(list);// 使用对象流把一个Java.lang.Object类的对象输出到一个流中（网络流，管道流，文件流）
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (objectOutputStream != null) {
				try {
					objectOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	// 编写一段代码，从文件中读取数据并还原为一组学生信息；
	public static void readDataFromFile() {
		File file = new File(FILE_NAME);
		if (!file.exists()) {
			System.out.println("亲，找不到文件: " + FILE_NAME);
			return;
		}
		FileInputStream inputStream = null;
		ObjectInputStream objectInputStream = null;

		try {
			inputStream = new FileInputStream(file);
			objectInputStream = new ObjectInputStream(inputStream);
			Object object = objectInputStream.readObject();
			List<Student> list = (List<Student>) object;// 向下溯型
			for (Student student : list) {
				System.out.println(student);
			}

		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (objectInputStream != null) {
				try {
					objectInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

	public static void main(String[] args) {
		// 编写一段代码，从键盘输入3个学生的信息，并保存到文件中
		inputStundentInfo();// 序列化对象到文件中
		// 编写一段代码，从文件中读取数据并还原为一组学生信息；
		readDataFromFile();
	}

}
